package ch.zbw.pra.sysventory.domain.businesslogic;

import java.util.ArrayList;
import java.util.List;

import ch.zbw.pra.sysventory.domain.dbconnector.DBConnectorCreator;
import ch.zbw.pra.sysventory.domain.dbconnector.MySQLConnector;
import ch.zbw.pra.sysventory.domain.dbhandling.SearchInScanversionsFromDB;
import ch.zbw.pra.sysventory.domain.dbhandling.StatisticData;
import ch.zbw.pra.sysventory.domain.model.Computer;
import ch.zbw.pra.sysventory.domain.model.Scanversion;
import ch.zbw.pra.sysventory.network.server.TCPServer;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.SettingsPanel;

public class DomainController {

	private UserManagement userManagement;

	private static DomainController domainController;
	private TCPServer tcpServer;
	DBConnectorCreator dbConnectorCreator;
	MySQLConnector mySQLConnector;
	private SearchInScanversionsFromDB searchInScanversionsFromDB;
	private StatisticData statisticData;
	private SettingsPanel settingsPanel;

	private DomainController() {
		this.userManagement = new UserManagement();
		this.dbConnectorCreator = new DBConnectorCreator();
		this.mySQLConnector = (MySQLConnector) this.dbConnectorCreator.createConnector();
		this.mySQLConnector.connect();
		this.tcpServer = new TCPServer(7777, this.mySQLConnector);
		new Thread(this.tcpServer).start();

		this.searchInScanversionsFromDB = new SearchInScanversionsFromDB(this.mySQLConnector);
		this.statisticData = new StatisticData(this.mySQLConnector);
	}

	public List<Computer> startSearchPC(String computer, String ip, String computerTyp, String processor, String os) {

		List<Scanversion> scanVersions = this.searchInScanversionsFromDB.searchForScanversionsInDB(computer, ip, computerTyp, processor, os);
		List<Computer> computers = new ArrayList<Computer>();
		for (Scanversion sv : scanVersions) {
			computers.add(sv.getComputer());
		}
		scanVersions = null;
		return computers;
	}

	public boolean checkUser(String username, String userpassword) {

		return this.userManagement.checkUser(username, userpassword);

	}

	public boolean checkHasInventorRights() {
		return this.userManagement.isHasInventorRights();
	}

	public void setScanStatus(boolean scanStatus) {
		if (scanStatus) {
			this.tcpServer.setScanning(scanStatus);
		} else {
			this.tcpServer.closeHandlers();
		}
	}

	public boolean isScanFinished() {
		return this.tcpServer.isScanFinished();
	}

	public static DomainController getInstance() {
		if (domainController == null) {
			domainController = new DomainController();
		}
		return domainController;
	}

	public int getOsCount() {
		return this.statisticData.countOS();
	}

	public int getSwCount() {
		return this.statisticData.countSoftwares();
	}

	public int getAmountWeekScans() {
		return this.statisticData.scansThisWeek();
	}

	public int getAmountMonthScans() {
		return this.statisticData.scansThisMonth();
	}

	public int getAmountYearScans() {
		return this.statisticData.scansThisYear();
	}

	public void receiveASettingsPanelReference(SettingsPanel settingsPanel) {
		this.settingsPanel = settingsPanel;
	}

	public SettingsPanel getSettingsPanel() {
		return this.settingsPanel;
	}
}
